/*
 *  hmlme.cp
 *  hmlme
 *
 *  Created by Zhou Vega on 10-4-21.
 *  Copyright 2010 __MyCompanyName__. All rights reserved.
 *
 */

//#include <iostream>
//#include "hmlme.h"
#include <Python.h>
#import <Carbon/Carbon.h>


void PostMouseEvent(CGMouseButton button, CGEventType type, const CGPoint point) 
{
	CGEventRef theEvent = CGEventCreateMouseEvent(NULL, type, point, button);
	//CGEventSetType(theEvent, type);
	CGEventPost(kCGHIDEventTap, theEvent);
	CFRelease(theEvent);
}

void LeftClick(const CGPoint point) 
{
	PostMouseEvent(kCGMouseButtonLeft, kCGEventMouseMoved, point);
	PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseDown, point);
	PostMouseEvent(kCGMouseButtonLeft, kCGEventLeftMouseUp, point);
}

void RightClick(const CGPoint point) 
{
	PostMouseEvent(kCGMouseButtonRight, kCGEventMouseMoved, point);
	PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseDown, point);
	PostMouseEvent(kCGMouseButtonRight, kCGEventRightMouseUp, point);
}

void DoubleClick(const CGPoint point) {  
    /*CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);  
    CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    
    CGEventSetType(theEvent, kCGEventLeftMouseDown);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseDown);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp); 
    CGEventPost(kCGHIDEventTap, theEvent); 
    
    CFRelease(theEvent); */
    CGEventRef theEvent = CGEventCreateMouseEvent(NULL, kCGEventLeftMouseDown, point, kCGMouseButtonLeft);  
    CGEventPost(kCGHIDEventTap, theEvent); 
    
    CGEventSetType(theEvent, kCGEventLeftMouseUp);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    
    CGEventSetIntegerValueField(theEvent, kCGMouseEventClickState, 2);  
    
    
    CGEventSetType(theEvent, kCGEventLeftMouseDown);  
    CGEventPost(kCGHIDEventTap, theEvent);  
    CGEventSetType(theEvent, kCGEventLeftMouseUp); 
    CGEventPost(kCGHIDEventTap, theEvent); 
    
    
    CFRelease(theEvent);
}

/**/
void MoveMouse2(const CGPoint point) 
{
	PostMouseEvent(kCGMouseButtonRight, kCGEventMouseMoved, point);
}


PyObject *HML_LeftClick(PyObject *self, PyObject *args) {
    int x, y;
    if(!PyArg_ParseTuple(args, "ii", &x, &y)){
		return NULL;
    }
	
	CGPoint pt;
	pt.x = x;
	pt.y = y;
	LeftClick(pt);
	
    Py_INCREF(Py_None);
    return Py_None;
}


PyObject *HML_RightClick(PyObject *self, PyObject *args) {
    int x, y;
    if(!PyArg_ParseTuple(args, "ii", &x, &y)){
		return NULL;
    }
	
	CGPoint pt;
	pt.x = x;
	pt.y = y;
	RightClick(pt);
	
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *HML_DoubleClick(PyObject *self, PyObject *args) {
    int x, y;
    if(!PyArg_ParseTuple(args, "ii", &x, &y)){
		return NULL;
    }
	
	CGPoint pt;
	pt.x = x;
	pt.y = y;
	DoubleClick(pt);
	
    Py_INCREF(Py_None);
    return Py_None;
}

/**/
PyObject *HML_MoveMouse2(PyObject *self, PyObject *args) {
    int x, y;
    if(!PyArg_ParseTuple(args, "ii", &x, &y)){
		return NULL;
    }
	
	CGPoint pt;
	pt.x = x;
	pt.y = y;
	MoveMouse2(pt);
	
    Py_INCREF(Py_None);
    return Py_None;
}


static PyMethodDef hmlmemethods[] = {
    { "click", (PyCFunction)HML_LeftClick, METH_VARARGS, "Left click on the screen\n" },
    { "rightclick", (PyCFunction)HML_RightClick, METH_VARARGS, "Right click on the screen\n" },
    { "dbclick", (PyCFunction)HML_DoubleClick, METH_VARARGS, "Left click on the screen\n" },
	{ "mmouse2", (PyCFunction)HML_MoveMouse2, METH_VARARGS, "Move Mouse on the screen\n" },
    { NULL, NULL },
};

void inithmlme(void)
{
    Py_InitModule("hmlme",hmlmemethods);
}
